package com.caifeng.goodstwo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 简单购物车
 * 实现
 * 添加到购物车
 * 加减数目 删除商品
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView tname, tname2;
    ImageView jia, car, jia2;
    MyHelp myHelp;
    TextView price, price2;
    String SUCCESS = "成功";
    String FAIL = "失败";
    String ADD = "已加入到购物车";
    String MORE = "再次添加成功";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }

    private void init() {
        jia = findViewById(R.id.jiaru);
        tname = findViewById(R.id.name);
        jia2 = findViewById(R.id.jiaru2);
        tname2 = findViewById(R.id.name2);
        price = findViewById(R.id.price);
        price2 = findViewById(R.id.price2);
        car = findViewById(R.id.shop);
        jia.setOnClickListener(this);
        jia2.setOnClickListener(this);
        car.setOnClickListener(this);
        myHelp = new MyHelp(this);

    }

    public void show(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    private void insertTo(String id, String name, double p) {
        if (myHelp.getNum(id) != 0) {
//                if (id != null) {
            if (myHelp.updateData(id,"add")) {
                show(MORE);
                Log.e(SUCCESS, "更新");
            } else {
                show(FAIL);
                Log.e(FAIL, FAIL + "2");
            }
        } else {

            if (myHelp.insertData(id,name, "1", p)) {
                show(ADD);
                Log.e(SUCCESS, "添加");
            } else {
                show(FAIL);
                Log.e(FAIL, "添加");
            }
        }
    }

    /**
     *获取价格
     */
    public double getPrice(String s) {
        String str = s.substring(1, s.length() - 1);
        double price = Double.valueOf(str);
        return price;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jiaru:
                //商品名称
                String gname = String.valueOf(tname.getText());
                String one = "1";
                String strP = String.valueOf(price.getText());
                insertTo(one, gname, getPrice(strP));

                break;
            case R.id.jiaru2:
                String gname2 = String.valueOf(tname2.getText());
                String two = "2";
//                if (id != null) {
//                if (myHelp.getNum(two) != 0) {
//                    if (myHelp.updateData(two)) {
//                        Log.e(SUCCESS, "更新");
//                    } else {
//                        Log.e(FAIL, "添加");
//                    }
//                } else {
//                    if (myHelp.insertData(gname2, "1")) {
//                        Log.e(SUCCESS, "添加");
//                    } else {
//                        Log.e(FAIL, "添加");
//                    }
//                }
//                Log.e("1111", gname2);
                String strP2 = String.valueOf(price2.getText());

                insertTo(two, gname2, getPrice(strP2));
                break;

            case R.id.shop:
                Intent intent = new Intent(MainActivity.this, Shop.class);
                startActivity(intent);
                break;
        }
    }
}
